var _config ;
var faultphoto = [];//拍照列表
var starvalue=0;
var labelres;
var pingjiaid;
var starres = [];
var starArray=[];
var starJson = {};
var starlength=1;
$(document).ready(function(){
	if(GetQueryString("pandianid") != null){
		//$('#choose').style.display="none";
		$("#choose").hide();
	}
	/*if(type=='1'){
		$('#touxiang').text(decodeURI(decodeURI(username)));
	}else{
		$('#touxiang').text(decodeURI(decodeURI(username)).substring(decodeURI(decodeURI(username)).length-2,decodeURI(decodeURI(username)).length));
	}*/
	$('#username').text(decodeURI(decodeURI(username)));
	getzuzhi(userId);
	gettouxiang(userId);
	getid();
	getstardiv();
	getstar();
	getlabel();
	getlabel_dict();
});	
function gettouxiang(userId){
	$.ajax({  
		url: "../../unite_platform/mobile_pingjia/get_touxiang"
		,type:'post'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify({"userid":userId}) 
		,dataType:'json'
		,success: function(res){
			if(res.data.length>0 && res.data[0].photo != '' && res.data[0].photo != undefined){
				$('#touxiang').append('<img class="w100 add_rad_8p h70p w70p" src="'+res.data[0].photo+'" />');
			}else{
				$('#touxiang')[0].className = 'w70p h70p add_rad_10p  white add_mar_r2 fs_14p align_center add_weight bg_blue';
				if(type=='1'){
					$('#touxiang').text(decodeURI(decodeURI(username)));
				}else{
					$('#touxiang').text(decodeURI(decodeURI(username)).substring(decodeURI(decodeURI(username)).length-2,decodeURI(decodeURI(username)).length));
				}
				//$('#touxiang').text(sessionStorage.getItem("name").substring(sessionStorage.getItem("name").length-2,sessionStorage.getItem("name").length));
			}
		}
	});
}
function getid(){
	$.ajax({  
		url: "../../unite_platform/mobile_pingjia/fillid"
		,type:'post'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify({}) 
		,dataType:'json'
		,success: function(res){
			pingjiaid = res.data.field.id;
		}
	});
}
function getzuzhi(userId){
	$.ajax({  
		url: "../../unite_platform/mobile_pingjia/get_zuzhi"
		,type:'post'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify({"userid":userId})  
		,dataType:'json'
		,success: function(res){
			if(res.data.length>0){
				for(var i =0;i<res.data.length;i++){
				if(i==0)$('#zuzhi').append('<span>'+res.data[i].name.substring(0,4)+'</span>');
				else $('#zuzhi').append('<span>--'+res.data[i].name+'</span>');
				}
			}else{
				$('#zuzhi').append('<span>无</span>');
			}	
		}
	});
}
function getstardiv(){
	$.ajax({  
		url: "../../unite_platform/mobile_pingjia/getstardiv"
		,type:'post'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify({"userId":userId}) 
		,dataType:'json'
		,success: function(res){
			console.log(res);
			starres = res.data;
			starlength =res.data.length;
			for(var i=0;i<res.data.length;i++){
				starJson.id=pingjiaid;
				starJson.star=res.data[i].id;
				starJson.starname=res.data[i].name;
				starJson.starvalue=0;
				starArray.push(starJson);
				starJson = {};
//				$('#stardiv').append('<div class="w94 add_pad_lr3 fs_14p add_weight add_mar_t5">'+res.data[i].name+'</div>'
//						+'<div class="w50 add_mar_lrauto add_mar_t5 add_mar_b5 align_center">'
//						+'<div id="star'+res.data[i].id+'"></div></div>');	
				$('#stardiv').append('<div class="w94 add_pad_lr3 add_mar_t5 add_mar_b5 fle_content_row fle_just_start fle_alig_center">'
									+'<div class="fs_14p add_mar_r5">'+res.data[i].name+'</div>'
									+'<div class="align_center">'
									+'<div id="star'+res.data[i].id+'"></div></div></div>');	
					starres[i].value = 0;
					layui.use(['rate'], function(){
					 var rate = layui.rate;
					 var theid = res.data[i].id;
					 var iid = i;
					 	 rate.render({
						    elem: '#star'+res.data[i].id
					 		,text: false
							,choose: function(value){
								var beforestar =starArray[iid].starvalue;
								starArray[iid].starvalue = value
						   		starvalue = starvalue+value-beforestar;
						 	}
						 })
					});
			}
		}
	});
}
function getstar(){
	$.ajax({  
		url: "../../unite_platform/mobile_pingjia/getstar"
		,type:'post'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify({"userId":userId}) 
		,dataType:'json'
		,success: function(res){
				layui.use(['rate'], function(){
				  var rate = layui.rate;
				  //基础效果
				  rate.render({
				    elem: '#star' 
			 		,text: false
					,readonly: true
					,value:res.data[0].star
					,theme: '#FC284C'
				 })
				});
		}
	});
/*	layui.use(['rate'], function(){
	  var rate = layui.rate;
 	 rate.render({
	    elem: '#star1' 
 		,text: false
		 ,choose: function(value){
	   			starvalue = value;
	 	}
	 })
	rate.render({
	    elem: '#star2' 
 		,text: false
		 ,choose: function(value){
	   			starvalue = value;
	 	}
	 })
	});*/
}
function getlabel(){
	$.ajax({  
		url: "../../unite_platform/mobile_pingjia/get_label"
		,type:'post'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify({"userId":userId}) 
		,dataType:'json'
		,success: function(res){
			if(res.data.length>0){
				for(var i=0;i<2;i++){
					if(res.data[i] != undefined && res.data[i].label_name != undefined){
						$('#label').append('<div class="add_inlblo add_mar_r2"><i class="iconfont fs_12p orange">&#xe620;</i><span class="fs_12p fs_6rem gray_dark">'+res.data[i].label_name+'</span></div>');
					}
				}
				if(res.data.length>0){
					$('#label').append('<div class="fle_gro_1 align_right fs_12p blue" onclick="pingjia_detail()">'+res.data[0].sum+'评价<i class="iconfont fs_12p">&#xe611;</i></div>');
				}
			}
		}
	});
}
function pingjia_detail(){
	 var  url = "pingjia_detail?userId="+userId;//此处拼接内容
	 window.location.href = url;
}
function fanhui(){
	 var  url = "toindex?corp="+sessionStorage.getItem("corp");//此处拼接内容
	 window.location.href = url;
}
function getlabel_dict(){
	$.ajax({  
		url: "../../unite_platform/mobile_pingjia/get_label_dict"
		,type:'post'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify({"userId":userId}) 
		,dataType:'json'
		,success: function(res){
			console.log(res);
			labelres = res.data;
			for(var i=0;i<res.data.length;i++){
				$('#label_dict').append(
					'<div  class="add_inlblo flo_left h26p add_pad_lr3 add_rad_4p fs_13p add_mar_r2 add_mar_t2 add_bor_gray gray" id="label'+res.data[i].id+'" onclick="change(\''+res.data[i].id+'\')">'+res.data[i].name+'</div>'
				);
			}
			$('#label_dict').append('<div class="clearfix"></div>');
			
		}
	});
}
//打开钉钉拍照
function openphoto(){
	if(_config == undefined){
		//alert(sessionStorage.getItem("corp"))
		$.ajax({
			type : "post",
			data:JSON.stringify({"pageUrl":location.href.replace(/\\&/g,'%26'),"corp":sessionStorage.getItem("corp")}),
			contentType: "application/json; charset=utf-8",
			async: false,
			url : "../dingding/getconfig",
			dataType : "json",
			success : function(suc){
				_config = suc.config;		
			}	
		}); 
		dd.config({
			agentId : _config.agentid,
			corpId : _config.corpId,
			timeStamp : _config.timeStamp,
			nonceStr : _config.nonceStr,
			signature : _config.signature,
			jsApiList : [ 'runtime.info','biz.util.uploadImage' ]
		});
	}
	dd.ready(function(){
		dd.biz.util.uploadImage({
		    compression: false,//(是否压缩，默认为true)
		    quality: 100, // 图片压缩质量, 
		    resize: 100, // 图片缩放率
		    stickers: {   // 水印信息
		        time: getNowFormatDateTime(),
		        username: username
		    },
		    onSuccess : function(result) {
			faultphoto.push(result[0]);
			$('#faultpicUrl').append('<div class="w50p h50p add_bor_gray add_rad_4p add_pos_re ">'+
									'<img class="w100 h100 add_rad_4p" src="'+result[0]+'" onclick="showAllPic(\''+result[0]+'\')" />'+
									'<div class="w20p h20p align_center add_pos_ab ab_t0r0 bg_black_opacity white fs_18p add_rad_04p010p" onclick="deletePhoto(this)">×</div>'+
									'</div>'
									);
		    },
		    onFail : function(err) {
			// alert(err)
			}
		});
	});
	dd.error(function(err) {
		alert('dd error: ' + JSON.stringify(err));
	});
}
//预览图片
function showAllPic(imgOne){
	dd.biz.util.previewImage({
		urls: faultphoto,//图片地址列表
		current: imgOne,//当前显示的图片链接
		onSuccess : function(result) {
			/**/
		},
		onFail : function(err) {}
	})
}	
function change(id){
	var theid = 'label'+id;
	if($('#'+theid)[0].className=='add_inlblo flo_left h26p add_pad_lr3 add_rad_4p fs_13p add_mar_r2 add_mar_t2 add_bor_gray gray'){
		$('#'+theid)[0].className='add_inlblo flo_left h26p add_pad_lr3 add_rad_4p fs_13p add_mar_r2 add_mar_t2 add_bor_orange orange';
	}else{
		$('#'+theid)[0].className='add_inlblo flo_left h26p add_pad_lr3 add_rad_4p fs_13p add_mar_r2 add_mar_t2 add_bor_gray gray';
	}
}
function saveinfo(){
	var sendData = {};
	var insertArray = [];
	sendData.infoData={};
	sendData.infoData.id=pingjiaid;
	sendData.infoData.pjId=userId;
	sendData.infoData.type=type;
	console.log(starvalue);
	console.log(starlength);
	console.log(Math.round(starvalue/starlength));
	sendData.infoData.star=Math.round(starvalue/starlength);
	sendData.infoData.pic=faultphoto.join(',');
	sendData.infoData.lrr=sessionStorage.getItem("userId");
	sendData.infoData.content=$('#textArea').val();
	sendData.infoData.anonymous=$('#niming')[0].checked==true?0:1;
	var insertJson = {};
	for(var i=0;i<labelres.length;i++){
		if($('#label'+labelres[i].id)[0].className =='add_inlblo flo_left h26p add_pad_lr3 add_rad_4p fs_13p add_mar_r2 add_mar_t2 add_bor_orange orange'){
			insertJson.id=pingjiaid;
			insertJson.label=labelres[i].id;
			insertJson.label_name=labelres[i].name;
			insertArray.push(insertJson);
			insertJson = {};
		}
	}
	sendData.insertArray = insertArray;
	sendData.starArray = starArray;
	if(starvalue == 0){
		alert("评分不能为空!");
//	}else if($('#textArea').val() =="" || $('#textArea').val() ==undefined){
//		alert("评价内容不可为空!");
	}else{
		$.ajax({  
			url: "../../unite_platform/mobile_pingjia/Add"
			,type:'post'
			,async: false
			,contentType: "application/json; charset=utf-8"
			,data:JSON.stringify(sendData) 
			,dataType:'json'
			,success: function(res){
				if(res.status=='1002'){
					layer.msg('操作成功',{icon:1,time:3000}); 
					 var  url = "toindex?corp="+sessionStorage.getItem("corp");//此处拼接内容
					 window.location.href = url;
				}else{
					layer.msg('评价失败',{icon:1,time:3000}); 
					 var  url = "toindex?corp="+sessionStorage.getItem("corp");//此处拼接内容
				 	window.location.href = url;
				}
				
			}
		});
	}
	if(GetQueryString("pandianid") != null ){
		$.ajax({  
			url: "../../unite_platform/mobile_page/pingjia/updateTypePingjia"
			,type:'post'
			,async: false
			,contentType: "application/json; charset=utf-8"
			,data:JSON.stringify({"pingjiaId":pingjiaid,"pingjiaType":GetQueryString("pingjiaType"),"id":GetQueryString("pandianid")}) 
			,dataType:'json'
			,success: function(res){
				
			}
		});
	}

}
//删除图片
function deletePhoto(obj){
	dd.device.notification.confirm({
	    message: "删除图片吗",
	    title: "提示",
	    buttonLabels: ['是', '否'],
	    onSuccess : function(result) {
	    	if(result.buttonIndex == '0'){
	    		var index = $(obj).parent().index()-1;
	    		$(obj).parent().remove();
	    		faultphoto.splice(index,1);
				
	    	}
	    },
	    onFail : function(err) {}
	});
	 
}
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}